<script type="text/javascript">

var paginationTotal =1;//记录总数
$(function(){
//	initData(0); //初始化分页<{$lang.data}>
	clear();
	
	$("#confirmDialog").dialog({
		autoOpen:false,
		modal:true,
		show:"slide",
		buttons:{
			"<{$lang.ok}>":function(){
				$(this).dialog("close");
			}
		}
	});
	
	$("#add").dialog({
		autoOpen:false,
		width:400,
		height:480,
		modal:true,
		show:"slide",
		buttons: {
			'<{$lang.ok}>': function() {
				save();
			},
	    	'<{$lang.cancel}>': function() {
				$(this).dialog('close');
			 }
		}
	});
	
	$("#right_confirm").dialog({
		autoOpen:false,
		modal:true,
		height:160,
		show:"slide",
		buttons: {
			'<{$lang.ok}>': function() {
				loading();
				$.ajax({
					type: "post",
					async: false,
					dataType: "json",
					url: "/product/package/delete", 
					data: "id=" + Uid,
					success: function(json) { 
						if(json.ask==1){
							initData(0);
							messags = json.msg;
						}else{
							messags = json.msg;
						}
						$("#confirmDialog").html(messags).dialog("open");
					}
				});
				closeLoading();
				$(this).dialog('close');
			},
	    	'<{$lang.cancel}>': function() {
					$(this).dialog('close');
			 }
		}

	});
	
});

function loadData(page,pageSize){
	var package_name = $('[name=package_name]').val();
	var status = $('[name=status_sch]').val();
	loading();
	$.ajax({ //这里使用到Jquery的ajax<{$lang.method}>
		type: "POST",
		async: false,
		dataType: "json",
		url: "/product/package/list", //请求的处理url
		data:{
			'page':page,	//分页的页数
			'pageSize':pageSize,
			'pp_name':package_name,
			'status':status
		},
		success: function(json) {
			var list = '';
			paginationTotal=json.total;
			if(json.ask != 1){
				list = "<td colspan='13' class=''>&nbsp;<{$lang.no_data}></td>";
			}else {
				$.each(json.result, function(key, val){				
					list += "<tr>";					
					list += "<td>"+(key+1)+"</td>";
					list += "<td>"+val.customer_id+"</td>";
					list += "<td>"+val.warehouse_id+"</td>";
					list += "<td>"+val.pp_name+"</td>";
					list += "<td>"+val.pp_length+"</td>";
					list += "<td>"+val.pp_width+"</td>";
					
					list += "<td>"+val.pp_height+"</td>";
					list += "<td>"+val.pp_weight+"</td>";
					list += "<td>"+val.pp_price+"</td>";
					list += "<td>"+val.currency_code+"</td>";
					if(val.status == 1){
						list += '<td><img src="/images/icon/statusok.png" title="<{$lang.useable}>"></td>';
					}else{
						list += '<td><img src="/images/icon/unnormal.gif" title="不<{$lang.useable}>"></td>';
					}					
					
					list += "<td>"+val.pp_add_time+"</td>";					
					list += "<td><a href=\"javascript:editPackage("+val.pp_id+");\"><img title='<{$lang.edit}>' src='/images/icon/edit.gif'></a>";				
					list += "| "+ "<a href=\"javascript:delelteInfo("+val.pp_id+")\"><img title='<{$lang.delete}>' src='/images/icon/delete.gif'></a></td>";
					
					list += "</tr>";
				});
				closeLoading();
			}
			$("#list").html(list);
		}
	}); 
}

function search(){	
	initData(0);
}

function editPackage(id){
		
	var html = '<input type="text" name="pp_name" id="pp_name" class="text ui-widget-content ui-corner-all"/>';
	$('#name-span').html(html);	
	clear();
	if(id){
		
		$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/product/package/single", //请求的处理url
			data:{'id':id},
			success: function(json) {
				if(json.ask == 1 ) {
					var val = json.result;
					$('[name=pp_id]').val(val.pp_id);
					$('#name-span').html(val.pp_name);
					$('[name=customer_id]').val(val.customer_id);
					$('[name=warehouse_id]').val(val.warehouse_id);
					$('[name=pp_length]').val(val.pp_length);
					$('[name=pp_width]').val(val.pp_width);
					
					$('[name=pp_height]').val(val.pp_height);
					$('[name=pp_weight]').val(val.pp_weight);
					$('[name=pp_price]').val(val.pp_price);
					$('[name=status]').val(val.status);
					$('[name=currency_code]').val(val.currency_code);
					
				}
				$("#add").dialog("open");
			}
		});
	}else{				
		$("#add").dialog("open");		
	}
}

function save(){
	vForm='';
	vForm = $("#createForm").serializeArray();
	
	var tip = '';
	$.ajax({
			type: "POST",
			async: false,
			dataType: "json",
			url: "/product/package/edit", //请求的处理url
			data:vForm,
			success: function(json) {
				$.each(json.msg,function(key,val){
					tip += val+'<br />';
				});
				
				loadData(paginationCurrentPage,paginationPageSize);
				$("#add").dialog("close");
				$("#confirmDialog").html(tip).dialog("open");				
			}
	});
}

function closebox(){
	$("#add").dialog("close");
}

//<{$lang.delete}>
function delelteInfo(id){
	if(id==''||id==undefined){
		return false;
	}
	Uid = id;
	$('#right_confirm').dialog('open');
}


function clear(){
	$('[name=package_name]').val('');		//必须<{$lang.clear}>，否则会报错
	$('[name=pp_id]').val('');
	$('[name=pp_name]').val('');
	$('[name=warehouse_id]').val('');
	$('[name=pp_length]').val('');
	
	$('[name=pp_width]').val('');
	$('[name=pp_height]').val('');
	$('[name=pp_weight]').val('');
	$('[name=pp_price]').val('');
	$('[name=customer_id]').val('');
}

</script>
<style type="text/css">
.button{background:url(images/left-menu-li.gif) repeat-x left top #F1F6F9; border:1px solid #318DAC; font-weight:bold; padding:2px 5px; font-weight:lighter; color:#318DAC; vertical-align:middle; -moz-border-radius:3px; -webkit-border-radius:3px; margin:auto 5px}
</style>

<div id="right_confirm" style="display: none;">
	<p style="text-align:center;font-size: 14px;color: red;font-weight: bold;">
	<{$lang.delete_sure}>？
	</p>
</div>

<div id="iTitle">
	<div id="iTitleSub">
	
		<form id="sForm" name="sForm" onkeypress="if(event.keyCode==13){return false;}">
		<div>
		<label>
			<{$lang.packaging_material}><{$lang.name}>:<input id="package_name" class="myInput" type="text" onkeyup="if(event.keyCode==13){search();}" name="package_name">
		</label>&nbsp;
		<label>
			<{$lang.status}>:<select name="status_sch">
				<option value=""><{$lang.pls}></option>
				<option value="0"><{$lang.unuseable}></option><option value="1"><{$lang.useable}></option>
			</select>			
		</label>&nbsp;
		<input class="button" type="button" value="<{$lang.search}>" onclick="search();">&nbsp;&nbsp;
		<input class="button" type="button" value="<{$lang.add}>"  id="addButton" onclick="editPackage('')">&nbsp;&nbsp;
		</div>
		</form>
		
	</div>
	<div id="message" style="color:red;"></div>
	<div class="myClear"></div>
</div><!-- id="iTitle" 结束 -->

<h3><{$lang.packaging_material}><{$lang.list}></h3>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">	
	<thead><tr>
    	<th>ID</th>
    	<th><{$lang.customer_code}></th>
    	<th><{$lang.warehouse}></th>
   		<th><{$lang.name}></th>
    	<th><{$lang.length}></th>
        <th><{$lang.width}></th>
        
        <th><{$lang.height}></th>
        <th><{$lang.weight}></th>
        <th><{$lang.price}></th>
        <th><{$lang.currency}><{$lang.code}></th>
        <th><{$lang.useable}>/<{$lang.unuseable}></th>
        <th><{$lang.add}><{$lang.date}></th>
        
        <th><{$lang.operation}></th>
	</tr></thead>
    
	<tbody id="list"></tbody>
	
</table>

<div class="pagination"></div>

<style type="text/css">
div#add{
	text-align:center;
}
div.info{
	text-align:left;
}
div.item{
	margin-bottom: 12px;
}
input.text {
    height: 22px;    
    margin-left: 8px;
}
span.text{
	margin-left: 8px;
}
span.userInfoLeft{
	display:block;
	float:left;
	width:100px;
}
</style>

<div id="add" title="<{$lang.packaging_material}><{$lang.msg}><{$lang.edit}>" style="display:none;">
 
	<div class="info">
		<div class="validateTips" id="tips"></div>
		
		<form id="createForm" name="createForm" >
		<input type="hidden" name="pp_id" id="pp_id" />
		<div class="item">
			<span class="userInfoLeft"><{$lang.packaging_material}><{$lang.name}>:</span>
			<span class="userInfoRight" id="name-span">
							
			</span>
			<div style="clear:both;"></div>
		</div>
		<div class="item">
			<span class="userInfoLeft"><{$lang.customer_code}>:</span>
			<span class="userInfoRight">
				<input type="text" name="customer_id" id="customer_id" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear:both;"></div>
		</div>
		
		<div class="item">
			<span class="userInfoLeft"><{$lang.warehouse}>:</span>
			<span class="text userInfoRight">
				<select name="warehouse_id" id="warehouse_id" class="ui-widget-content ui-corner-all" >
					<option value='1'>SZ</option>
				</select>		
			</span>
			<div style="clear: both;"></div>
		</div>
		
		<div class="item">
			<span class="userInfoLeft"><{$lang.length}>:</span>
			<span class="userInfoRight">
				<input type="text" name="pp_length" id="pp_length" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>		
		<div class="item">
			<span class="userInfoLeft"><{$lang.width}>:</span>
			<span class="userInfoRight">
				<input type="text" name="pp_width" id="pp_width" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>
		<div class="item">
			<span class="userInfoLeft"><{$lang.height}>:</span>
			<span class="userInfoRight">
				<input type="text" name="pp_height" id="pp_height" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>
		<div class="item">
			<span class="userInfoLeft"><{$lang.weight}>:</span>
			<span class="userInfoRight">
				<input type="text" name="pp_weight" id="pp_weight" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>	
		
		<div class="item">
			<span class="userInfoLeft"><{$lang.price}>:</span>
			<span class="userInfoRight">
				<input type="text" name="pp_price" id="pp_price" class="text ui-widget-content ui-corner-all"/>
			</span>
			<div style="clear: both;"></div>
		</div>
		
		<div class="item">
			<span class="userInfoLeft"><{$lang.status}>:</span>
			<span class="text userInfoRight">
				<select name="status" id="status" class="ui-widget-content ui-corner-all" >
					<option value='0'><{$lang.unuseable}></option>
					<option value='1' selected><{$lang.useable}></option>
				</select>		
			</span>
			<div style="clear: both;"></div>
		</div>
<!--		<{if $item.currency_code eq 'RMB'}>selected<{/if}>-->
		<div class="item">
			<span class="userInfoLeft"><{$lang.currency}>code:</span>
			<span class="text userInfoRight">
				<select name="currency_code" id="currency_code" class="ui-widget-content ui-corner-all">					
					<{foreach key=key item=item from=$currencyArr}>
						<{if $item.currency_code neq "RMB" }>
						<option value="<{$item.currency_code}>"><{$item.currency_cn_name}></option>
						<{/if}>						
					<{/foreach}>
					<option value="RMB" selected="selected" ><{$lang.RMB}></option>
				</select>
			</span>
			<div style="clear: both;"></div>
		</div>
		
		</form>
	
	</div>
</div>

<div id="confirmDialog" title="<{$lang.system}><{$lang.msg}>">	</div>